import json
import time
import scrapy
from njt_scrapy.settings import USER_NAME, PASSWORD
import tkinter as tk

# 创建一个简单的窗口
root = tk.Tk()
root.withdraw()  # 隐藏主窗口


class NbxtSpiderSpider(scrapy.Spider):
    name = "nbxt_spider"
    # allowed_domains = ["nbxt.njtsys.com"]
    start_urls = ["https://nbxt.njtsys.com/api/nbxt/nb/wo/get-workorder-list"]
    # start_urls = ["http://localhost:20014/nbxt/nb/wo/get-workorder-list"]
    pageNo = 1
    pageSize = 40

    def start_requests(self):
        url = 'https://nbxt.njtsys.com/api/comm/auth/login'
        # url = 'http://localhost:20014/comm/auth/login'
        body = {
            'username': USER_NAME,
            'password': PASSWORD
        }
        yield scrapy.FormRequest(url=url, callback=self.login_after, body=json.dumps(body), method='POST')

    def login_after(self, response):
        app_token = json.loads(response.body).get('appToken')
        if app_token:
            self.crawler.stats.set_stats({'app_token': app_token})
            formdata = {
                'searchType': '未接单的',
                'majorClass': '开发',
                'isWoStateOperateDone': 'false',
                'isWoStateCancel': 'false',
                'isWoStateFinish': 'false',
                'isWoStateInitial': 'true',
                'isWoStateTake': 'false',
                'isWoStateTest': 'false',
                'isDescending': 'false',
                'pageNo': str(self.pageNo),
                'pageSize': str(self.pageSize)
            }
            yield scrapy.FormRequest(url=self.start_urls[0], callback=self.parse, formdata=formdata, method='GET')
        else:
            self.crawler.engine.close_spider(self, '未找到token====================================')

    def parse(self, response):
        workorderList = json.loads(response.body).get('workorderList', [])
        if len(workorderList) > 0:
            for workorder in workorderList:
                woId = workorder.get('woId', None)
                title = str(workorder.get('title', None))
                approverName = str(workorder.get('approverName', None))
                print(woId, title, approverName)
                if (title.find('前端') > -1) and (approverName.find('李钰') > -1):
                    # 显示弹窗
                    self.show_popup(woId, title)
                    timestamp_ms = int(round(time.time() * 1000))
                    # 获取验证码
                    yield scrapy.FormRequest(
                        url=f'https://nbxt.njtsys.com/api/nbxt/nb/wo/get-wo-captcha?{timestamp_ms}',
                        callback=self.common_verifty, method='GET', meta={'woId': woId})

        time.sleep(10)
        formdata = {
            'searchType': '未接单的',
            'majorClass': '开发',
            'isWoStateOperateDone': 'false',
            'isWoStateCancel': 'false',
            'isWoStateFinish': 'false',
            'isWoStateInitial': 'true',
            'isWoStateTake': 'false',
            'isWoStateTest': 'false',
            'isDescending': 'false',
            'pageNo': str(self.pageNo),
            'pageSize': str(self.pageSize)
        }
        yield scrapy.FormRequest(url=self.start_urls[0], callback=self.parse, formdata=formdata, method='GET',
                                 dont_filter=True)
